package com.cmrx.model.util;

import com.cmrx.model.entity.User;
import com.cmrx.model.vo.LogVo;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志
 */
public class LogUtil {

    public static boolean WriteLogByLogVo(LogVo logVo){
        String info = getString(logVo);
        String logPath = PropertyUtil.getValue("my_log_url").trim();
        try {
            File logFlie = new File(logPath);
            if(!logFlie.exists()){
                logFlie.createNewFile();
            }
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logPath,true),"utf-8"));
            out.write(info);
//            out.newLine();
            out.flush();
            out.close();
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public static boolean WriteLogByParam(HttpServletRequest request,User user,String opType,String operateCondition,String operateResult,String querySql){

        LogVo logVo = new LogVo();
        logVo.setLogID(UUIDUtil.getUUID());
        logVo.setOperatorAccount(user.getUserCard()==null?"":user.getUserCard());
        logVo.setOperatorName(user.getUserName()==null?"":user.getUserName());
        logVo.setOrganizationName(user.getOrganName()==null?"":user.getOrganName());
        logVo.setOrganizationID(user.getRegionalism()==null?"":user.getRegionalism());

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String time = sdf.format(date);
        logVo.setOpTime(time);
        logVo.setResTime(time);
        logVo.setOpType(opType);
        logVo.setOperateCondition(operateCondition==null?"":operateCondition);
        logVo.setOperateResult(operateResult==null?"1":operateResult);

        String url = request.getRequestURI();
        logVo.setClientIp(request.getRemoteAddr());
        logVo.setClientPort(request.getRemotePort()+"");
        logVo.setUrl(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+url+"/");
        logVo.setSessionID(request.getSession().getId());

        if(url.startsWith("/xk_model/analysis/")){
            logVo.setFuncModuleName("分析意见");
        }else if(url.startsWith("/xk_model/case/")){
            logVo.setFuncModuleName("基本信息");
        }else if(url.startsWith("/xk_model/login/")){
            logVo.setFuncModuleName("用户相关");
        }else if(url.startsWith("/xk_model/scenephoto/")){
            logVo.setFuncModuleName("现场照片");
        }else if(url.startsWith("/xk_model/picture/")){
            logVo.setFuncModuleName("现场方位图");
        }else if(url.startsWith("/xk_model/sign/")){
            logVo.setFuncModuleName("单点登录");
        }else if(url.startsWith("/xk_model/sysdict/")){
            logVo.setFuncModuleName("字典表");
        }else {
            logVo.setFuncModuleName("其他");
        }

        String serverName = request.getServerName();//localhost
        int serverPort = request.getServerPort();//8080

        logVo.setObjectIP(serverName);
        logVo.setObjectPort(serverPort+"");
        logVo.setQuerySql(querySql);

        String info = getString(logVo);
        String logPath = PropertyUtil.getValue("my_log_url").trim();
        try {
            File logFlie = new File(logPath);
            if(!logFlie.exists()){
                logFlie.createNewFile();
            }
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logPath,true),"utf-8"));
            out.write(info);
//            out.newLine();
            out.flush();
            out.close();
        }catch (Exception e){
            return false;
        }
        return true;
    }


    public static boolean WriteLogByParam(HttpServletRequest request,User user,String opType,String operateCondition,String operateResult,String querySql,String errCode){

        LogVo logVo = new LogVo();
        logVo.setLogID(UUIDUtil.getUUID());
        logVo.setOperatorAccount(user.getUserCard()==null?"":user.getUserCard());
        logVo.setOperatorName(user.getUserName()==null?"":user.getUserName());
        logVo.setOrganizationName(user.getOrganName()==null?"":user.getOrganName());
        logVo.setOrganizationID(user.getRegionalism()==null?"":user.getRegionalism());

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String time = sdf.format(date);
        logVo.setOpTime(time);
        logVo.setResTime(time);
        logVo.setOpType(opType);
        logVo.setOperateCondition(operateCondition==null?"":operateCondition);
        logVo.setOperateResult(operateResult==null?"1":operateResult);

        String url = request.getRequestURI();
        logVo.setClientIp(request.getRemoteAddr());
        logVo.setClientPort(request.getRemotePort()+"");
        logVo.setUrl(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+url+"/");
        logVo.setSessionID(request.getSession().getId());

        if(url.startsWith("/xk_model/analysis/")){
            logVo.setFuncModuleName("分析意见");
        }else if(url.startsWith("/xk_model/case/")){
            logVo.setFuncModuleName("基本信息");
        }else if(url.startsWith("/xk_model/login/")){
            logVo.setFuncModuleName("用户相关");
        }else if(url.startsWith("/xk_model/scenephoto/")){
            logVo.setFuncModuleName("现场照片");
        }else if(url.startsWith("/xk_model/picture/")){
            logVo.setFuncModuleName("现场方位图");
        }else if(url.startsWith("/xk_model/sign/")){
            logVo.setFuncModuleName("单点登录");
        }else if(url.startsWith("/xk_model/sysdict/")){
            logVo.setFuncModuleName("字典表");
        }else {
            logVo.setFuncModuleName("其他");
        }

        String serverName = request.getServerName();//localhost
        int serverPort = request.getServerPort();//8080
        logVo.setErrCode(errCode);
        logVo.setObjectIP(serverName);
        logVo.setObjectPort(serverPort+"");
        logVo.setQuerySql(querySql);

        String info = getString(logVo);
        String logPath = PropertyUtil.getValue("my_log_url").trim();
        try {
            File logFlie = new File(logPath);
            if(!logFlie.exists()){
                logFlie.createNewFile();
            }
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logPath,true),"utf-8"));
            out.write(info);
//            out.newLine();
            out.flush();
            out.close();
        }catch (Exception e){
            return false;
        }
        return true;
    }



    public static String getString(LogVo logVo){
        StringBuffer result = new StringBuffer();
        result.append(logVo.getProductId());
        result.append("|");
        result.append(logVo.getProductName());
        result.append("|");
        result.append(logVo.getLogID());
        result.append("|");
        result.append(logVo.getOperatorID());
        result.append("|");
        result.append(logVo.getOperatorAccount());
        result.append("|");
        result.append(logVo.getOperatorIdentity());
        result.append("|");
        result.append(logVo.getOperatorName());
        result.append("|");
        result.append(logVo.getOrganizationName());
        result.append("|");
        result.append(logVo.getOrganizationID());
        result.append("|");
        result.append(logVo.getOpTime());
        result.append("|");
        result.append(logVo.getResTime());
        result.append("|");
        result.append(logVo.getTerminalType());
        result.append("|");
        result.append(logVo.getTerminalID());
        result.append("|");
        result.append(logVo.getTerminalNum());
        result.append("|");
        result.append(logVo.getTerminalMac());
        result.append("|");
        result.append(logVo.getOpType());
        result.append("|");
        result.append(logVo.getOperateCondition());
        result.append("|");
        result.append(logVo.getOperateResult());
        result.append("|");
        result.append(logVo.getErrCode());
        result.append("|");
        result.append(logVo.getClientIp());
        result.append("|");
        result.append(logVo.getClientPort());
        result.append("|");
        result.append(logVo.getUrl());
        result.append("|");
        result.append(logVo.getObjectParams());
        result.append("|");
        result.append(logVo.getSessionID());
        result.append("|");
        result.append(logVo.getFuncModuleName());
        result.append("|");
        result.append(logVo.getObjectIP());
        result.append("|");
        result.append(logVo.getObjectPort());
        result.append("|");
        result.append(logVo.getQuerySql());
        result.append("\r\n");
        return result.toString();
    }


}
